<template>
  <div>
    <el-form :model="form" :rules="rules" ref="ruleForm" class="form" size="medium" inline label-width="100px">
      <el-form-item label="车牌号" prop="num">
        <el-input v-model="form.num" placeholder="请输入车牌号"></el-input>
      </el-form-item>
      <el-form-item label="容量" prop="volume">
        <el-input v-model="form.volume" placeholder="请输入车辆容量">
          <template slot="append">m³</template>
        </el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="cancel" size="medium">取 消</el-button>
      <el-button type="primary" @click="confirm" size="medium" :loading="btnLoading">确 定</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      btnLoading:false,
      form:{
        num:"",
        volume:"",
        type:1,
      },
      rules:{
        num: [
          { required: true, message: '请输入车牌号', trigger: 'blur' }
        ],
        volume: [
          { required: true, message: '请输入车辆容量', trigger: 'blur' }
        ],
      }
    }
  },
  props:{
    formData:{
      type:Object,
      default:()=>{}
    }
  },
  watch:{
    formData:{
      handler(v){
        if(v){
          for(let k in this.form){
            if(k == 'gender'){
              this.form.gender = v.gender.toString()
            }else{
              this.form[k] = v[k]
            }
          }
        }else{
          this.$nextTick(()=>{
            this.form = {
              num:"",
              volume:"",
              type:1,
            }
          })
        }
      },
      deep:true,
      immediate:true
    }
  },
  methods:{
    cancel(){
      this.$emit('cancel')
    },
    confirm(){
      this.$refs['ruleForm'].validate(async (valid) => {
        if(valid){
          this.btnLoading = true
          let res = ""
          if(this.formData){
            res = await this.$api.editTruck(this.formData.id,this.form)
          }else{
            res = await this.$api.addTruck(this.form)
          }
          this.btnLoading = false
          this.$refs.ruleForm.resetFields()
          if(res){
            this.$emit('confirm')
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .dialog-footer{
    display: flex;
    justify-content: center;
    margin-top: 20px;
  }
</style>